import Func from './paint_plugin/function-picker/func'
import ColorPicker from './paint_plugin/color-picker'
import DataList from './paint_plugin/data-list'
import Utils from './utils'
import {EN,CN} from './config'
const InputDefine = {
	isItemWrapFocus(t){
		// let cn = this.cn
		// let pnode = t.parentNode
		// while(true){
		// 	if(pnode===this.$scope)return true
		// 	pnode = pnode.parentNode
		// }
	},
	scope($scope,$event){
		this.$scope = $scope
		this.$event = $event
		this.cp = new ColorPicker($event)
		this.dataList = new DataList($event)
		this.cip = null
		this.bind()
		this.redefine()
	},
	bind(){
		this.dataList.on(EN.onPick,(d)=>{
			// debugger
		})
		this.cp.on(EN.action,(data)=>{
			if(this.cip){
				let c16 = data.data.c16
				this.cip.value = c16
				this.cip.style.cssText=`background-color:${c16}33;color:${c16}`
				var evt = document.createEvent('HTMLEvents')
 					evt.initEvent('change', true, true)
 				this.cip.dispatchEvent(evt)
			}
			this.cp.focusOut()
		})
		this.$scope.addEventListener("click",(e)=>{
			let t = e.target
			let {x,y,top,height} = t.getBoundingClientRect()
			if(t.disabled)return
			this.cip = t
			if(t.dataset.itype==="color"){
				this.cp.focusIn({
					left:x,
					top:top+height
				})
			}
			if(t.dataset.itype==="datalist"){
				this.dataList.focusIn({
					target:t
				})
			}
		})
	},
	redefine(){
		let _this = this
		Reflect.defineProperty(HTMLInputElement.prototype,"nvalue",{
			get(){
				let defaultValue,itype = this.dataset.default
				let dtype = this.dataset.dtype
				let {type,value,checked} = this


				if(this.dataset.itype==="function")
					return Func.parseInput(value)
				if(this.dataset.itype==="datalist")
					return _this.dataListGet(this)
				switch(type){
					case "checkbox":
						return checked;
					case "number":
						return Number(value||defaultValue||0);
					case "text":
						return Utils.dataTransform(value.replace(/\s+/g,""),dtype)
					default:
						return value
				}
			},
			set(v){
				this.value = v
			}
		})
	},
	dataListGet(t){
		let v = t.value.replace(/\s+/g,"")
		let ds = Utils.dataTransform(t.nextElementSibling.innerHTML,"object")
		let nv = ds.filter(li=>li.title==v)[0].arg
		return Utils.dataTransform(nv,t.dataset.dtype)
	}
}
export default InputDefine